package pri.ydy.eduService.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pri.ydy.commonUtils.JwtUtils;
import pri.ydy.commonUtils.R;
import pri.ydy.commonUtils.ordervo.CourseInfoVoOrder;
import pri.ydy.eduService.client.OrdersClient;
import pri.ydy.eduService.entity.EduCourse;
import pri.ydy.eduService.entity.chapter.ChapterVo;
import pri.ydy.eduService.entity.frontvo.CourseFrontVo;
import pri.ydy.eduService.entity.frontvo.CourseWebVo;
import pri.ydy.eduService.service.EduChapterService;
import pri.ydy.eduService.service.EduCourseService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: YeDongYong
 * @Date:2020/12/25
 * @Content:
 */
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrdersClient ordersClient;

    //1.条件查询带分页
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit, @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> pagecourse=new Page<>(page,limit);
        Map<String ,Object> map= courseService.getTeacherFrontList(pagecourse,courseFrontVo);
        //返回当前页的所有数据
        return R.ok().data(map);
    }

    //2.课程详情查询
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id，编写SQL语句查询课程信息
        CourseWebVo courseWebVo= courseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);
        boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",buyCourse);
    }

    //根据课程id获得课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseInfoVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseInfoVoOrder courseInfoVoOrder=new CourseInfoVoOrder();
        BeanUtils.copyProperties(courseInfo,courseInfoVoOrder);
        return courseInfoVoOrder;
    }
}
